public class BinarySearchTree {

    static class TreeNode {
        public int key;
        public TreeNode left;
        public TreeNode right;

        TreeNode(int key) {
            this.key = key;
        }
    }

    public TreeNode root;

    /**
     * 插入一个元素
     *
     * @param key
     */
    public boolean insert(int key) {
        TreeNode newNode = new TreeNode(key);
        if (root == null) {
            root = newNode;
            return true;
        }
        TreeNode cur = root;
        TreeNode prev = null;
        while (cur != null) {
            if (key < cur.key) {
                prev = cur;
                cur = cur.left;
            } else if (key > cur.key) {
                prev = cur;
                cur = cur.right;
            } else {
                return false;
            }
        }
        if (key < prev.key) {
            prev.left = newNode;
        } else {
            prev.right = newNode;
        }
        return true;
    }

    //查找key是否存在
    public TreeNode search(int key) {
        TreeNode cur = root;
        while (cur != null) {
            if (key < cur.key) {
                cur = cur.left;
            } else if (key > cur.key) {
                cur = cur.right;
            } else {
                return cur;
            }
        }
        return null;
    }

    //删除key的值
    public boolean remove(int key) {

        TreeNode cur = root;
        TreeNode curP = null;
        while (cur != null) {
            if (key < cur.key) {
                curP = cur;
                cur = cur.left;
            } else if (key > cur.key) {
                curP = cur;
                cur = cur.right;
            } else {
                break;
            }
        }
        if (cur == null) {
            return false;
        }
        deleteNode(cur, curP);
        return false;
    }

    private void deleteNode(TreeNode cur, TreeNode Parent) {
        if (cur.left == null) {
            if (cur == root) {
                root = cur.right;
            } else {
                if (cur == Parent.left) {
                    Parent.left = cur.right;
                } else if (cur == Parent.right) {
                    Parent.right = cur.right;
                }
            }
        } else if (cur.right == null) {
            if (cur == root) {
                root = cur.left;
            } else {
                if (cur == Parent.left) {
                    Parent.left = cur.left;
                } else if (cur == Parent.right) {
                    Parent.right = cur.left;
                }
            }
        } else {
            //取左树的最大值替换cur.val
            TreeNode leftNode = cur.right;
            TreeNode leftNodeP = cur;
            while (leftNode.left != null) {
                leftNodeP = leftNode;
                leftNode = leftNode.left;
            }
            //
            cur.key = leftNode.key;
            if (leftNode == leftNodeP.left) {
                leftNodeP.left = leftNode.right;
            } else {
                leftNodeP.right = leftNode.right;
            }
        }
    }
}